home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 023 / ver30 / kbd.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  127 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Terminal independent keyboard handling.
  4.  * Version:    29
  5.  * Last edit:    05-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  */
  9. #include    "def.h"
  10.  
  11. /*
  12.  * Read in a key, doing the terminal
  13.  * independent prefix handling. The terminal specific
  14.  * "getkbd" routine gets the first swing, and may return
  15.  * one of the special codes used by the special keys
  16.  * on the keyboard. The "getkbd" routine returns the
  17.  * C0 controls as received; this routine moves them to
  18.  * the right spot in 11 bit code.
  19.  */
  20. getkey()
  21. {
  22.     register int    c;
  23.  
  24.     c = getkbd();
  25.     if (c == METACH)            /* M-            */
  26.         c = KMETA | getctl();
  27.     else if (c == CTRLCH)            /* C-            */
  28.         c = KCTRL | getctl();
  29.     else if (c == CTMECH)            /* C-M-            */
  30.         c = KCTRL | KMETA | getctl();
  31.     else if (c>=0x00 && c<=0x1F)        /* Relocate control.    */
  32.         c = KCTRL | (c+'@');
  33.     if (c == (KCTRL|'X'))            /* C-X            */
  34.         c = KCTLX | getctl();
  35.     return (c);
  36. }
  37.  
  38. /*
  39.  * Used above.
  40.  */
  41. getctl()
  42. {
  43.     register int    c;
  44.  
  45.     c = ttgetc();
  46.     if (ISLOWER(c) != FALSE)
  47.         c = TOUPPER(c);
  48.     if (c>=0x00 && c<=0x1F)            /* Relocate control.    */
  49.         c = KCTRL | (c+'@');
  50.     return (c);
  51. }
  52.  
  53. /*
  54.  * Transform a key code into a name,
  55.  * using a table for the special keys and combination
  56.  * of some hard code and some general processing for
  57.  * the rest. None of this code is terminal specific any
  58.  * more. This makes adding keys easier.
  59.  */
  60. VOID keyname(cp, k)
  61. register char    *cp;
  62. register int    k;
  63. {
  64.     register char    *np;
  65.     char        nbuf[3];
  66.  
  67.     static    char    hex[] = {
  68.         '0',    '1',    '2',    '3',
  69.         '4',    '5',    '6',    '7',
  70.         '8',    '9',    'A',    'B',
  71.         'C',    'D',    'E',    'F'
  72.     };
  73.  
  74.     if ((k&KCTLX) != 0) {            /* C-X prefix.        */
  75.         *cp++ = 'C';
  76.         *cp++ = '-';
  77.         *cp++ = 'X';
  78.         *cp++ = ' ';
  79.         k &= ~KCTLX;
  80.     }
  81.     if ((k&KCHAR)>=KFIRST && (k&KCHAR)<=KLAST) {
  82.         if ((np=keystrings[(k&KCHAR)-KFIRST]) != NULL) {
  83.             if ((k&KCTRL) != 0) {
  84.                 *cp++ = 'C';
  85.                 *cp++ = '-';
  86.             }
  87.             if ((k&KMETA) != 0) {
  88.                 *cp++ = 'M';
  89.                 *cp++ = '-';
  90.             }
  91.             strcpy(cp, np);
  92.             return;
  93.         }
  94.     }
  95.     if ((k&~KMETA) == (KCTRL|'I'))        /* Some specials.    */
  96.         np = "Tab";
  97.     else if ((k&~KMETA) == (KCTRL|'M'))
  98.         np = "Return";
  99.     else if ((k&~KMETA) == (KCTRL|'H'))
  100.         np = "Backspace";
  101.     else if ((k&~KMETA) == ' ')
  102.         np = "Space";
  103.     else if ((k&~KMETA) == 0x7F)
  104.         np = "Rubout";
  105.     else {
  106.         if ((k&KCTRL) != 0) {        /* Add C- mark.        */
  107.             *cp++ = 'C';
  108.             *cp++ = '-';
  109.         }
  110.         np = &nbuf[0];
  111.         if (((k&KCHAR)>=0x20 && (k&KCHAR)<=0x7E)
  112.         ||  ((k&KCHAR)>=0xA0 && (k&KCHAR)<=0xFE)) {
  113.             nbuf[0] = k&KCHAR;    /* Graphic.        */
  114.             nbuf[1] = 0;
  115.         } else {            /* Non graphic.        */
  116.             nbuf[0] = hex[(k>>4)&0x0F];
  117.             nbuf[1] = hex[k&0x0F];
  118.             nbuf[2] = 0;
  119.         }
  120.     }
  121.     if ((k&KMETA) != 0) {            /* Add M- mark.        */
  122.         *cp++ = 'M';
  123.         *cp++ = '-';
  124.     }
  125.     strcpy(cp, np);
  126. }
  127.